package com.bestcem.xm.user.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.enums.ReqErrorCodeEnum;
import com.bestcem.xm.common.core.enums.ResourceErrorCodeEnum;
import com.bestcem.xm.user.dao.OrganizationCustomSettingDao;
import com.bestcem.xm.user.entity.pojo.OrganizationCustomSettingDO;
import com.bestcem.xm.user.service.OrganizationCustomSettingService;
import com.bestcem.xm.user.service.dto.user.OrganizationCustomSettingDTO;
import com.bestcem.xm.user.util.convert.OrganizationCustomSettingConvert;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 租户自定义配置信息服务
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2022/5/10 11:04
 */
@Service
public class OrganizationCustomSettingServiceImpl implements OrganizationCustomSettingService {

    @Resource
    private OrganizationCustomSettingDao organizationCustomSettingDao;

    @Resource
    private OrganizationCustomSettingConvert organizationCustomSettingConvert;

    @Override
    public ServiceResult<OrganizationCustomSettingDTO> selectById(String id) {
        if (StringUtils.isBlank(id)) {
            return ServiceResult.fail(ReqErrorCodeEnum.PARAM_LOST, "配置id不能为空");
        }
        OrganizationCustomSettingDO organizationCustomSettingDO = organizationCustomSettingDao.selectById(id);
        if (Objects.isNull(organizationCustomSettingDO)) {
            return ServiceResult.fail(ResourceErrorCodeEnum.NOT_FOUND, "未查询到对应的租户自定义配置信息");
        }
        return ServiceResult.success(organizationCustomSettingConvert.do2Dto(organizationCustomSettingDO));
    }

    @Override
    public ServiceResult<List<OrganizationCustomSettingDTO>> selectBySelective(OrganizationCustomSettingDTO settingDTO) {
        if (Objects.isNull(settingDTO)) {
            return ServiceResult.fail(ReqErrorCodeEnum.PARAM_LOST, "参数信息不能为空");
        }
        List<OrganizationCustomSettingDO> organizationCustomSettingDOS = organizationCustomSettingDao
                .selectBySelective(organizationCustomSettingConvert.dto2Do(settingDTO));
        return ServiceResult.success(organizationCustomSettingConvert.dos2Dtos(organizationCustomSettingDOS));
    }

    @Override
    public ServiceResult<String> insertSelective(OrganizationCustomSettingDTO settingDTO) {
        if (Objects.isNull(settingDTO)) {
            return ServiceResult.fail(ReqErrorCodeEnum.PARAM_LOST, "参数信息不能为空");
        }
        String id = organizationCustomSettingDao.insertSelective(organizationCustomSettingConvert.dto2Do(settingDTO));
        return ServiceResult.success(id);
    }

    @Override
    public ServiceResult<Void> deleteById(String id) {
        if (StringUtils.isNotBlank(id)) {
            return ServiceResult.fail(ReqErrorCodeEnum.PARAM_LOST, "公钥私钥记录不能为空");
        }
        organizationCustomSettingDao.deleteById(id);
        return ServiceResult.success();
    }

    @Override
    public ServiceResult<OrganizationCustomSettingDTO> selectByOrgIdAndEnable(String orgId, boolean enable) {
        OrganizationCustomSettingDO settingDO = new OrganizationCustomSettingDO();
        settingDO.setOrgId(orgId);
        settingDO.setEnable(enable ? 1 : 0);
        List<OrganizationCustomSettingDO> organizationCustomSettingDos = organizationCustomSettingDao.selectBySelective(settingDO);
        if (CollectionUtils.isEmpty(organizationCustomSettingDos)) {
            return ServiceResult.success();
        }
        return ServiceResult.success(organizationCustomSettingConvert.do2Dto(organizationCustomSettingDos.get(0)));
    }

    @Override
    public ServiceResult<OrganizationCustomSettingDTO> selectBydomainAndEnable(String domain, boolean enable) {
        OrganizationCustomSettingDO settingDO = new OrganizationCustomSettingDO();
        settingDO.setDomain(domain);
        settingDO.setEnable(enable ? 1 : 0);
        List<OrganizationCustomSettingDO> organizationCustomSettingDos = organizationCustomSettingDao.selectBySelective(settingDO);
        if (CollectionUtils.isEmpty(organizationCustomSettingDos)) {
            return ServiceResult.success();
        }
        return ServiceResult.success(organizationCustomSettingConvert.do2Dto(organizationCustomSettingDos.get(0)));
    }

}
